package net.mlike.openssp.server.entity.system;

import net.mlike.openssp.server.entity.BaseEntity;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotEmpty;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by wallellen@hotmail.com on 5/24/18.
 */
@Entity
@Table(name = "t_sys_role")
public class Role extends BaseEntity {
    public enum Status {
        enabled, disabled
    }

    @NotEmpty(message = "名称不允许为空")
    private String name;

    private String code;

    private String description;

    @Enumerated(EnumType.STRING)
    private Status status;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "t_sys_role_menu",
        joinColumns = @JoinColumn(name = "role_id"),
        inverseJoinColumns = @JoinColumn(name = "menu_id")
    )
    private List<Menu> menus;

    @Transient
    public Set<String> getAllMenus() {
        Set<String> menus = new HashSet<>();

        if (this.menus == null) {
            return menus;
        }

        this.menus.forEach(m -> menus.add(m.getCode()));

        return menus;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(List<Menu> menus) {
        this.menus = menus;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
